/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.utils.validation;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.ui.dialogs.DiagnosticDialog;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
import org.eclipse.emf.edit.ui.action.ValidateAction;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.PlatformUI;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.preference.ValidationPreferenceConstants;

/**
 * This class is used to validate a model. All methods are static.
 * The code comes from Validation action {@link ValidateAction}
 * For moment marker are not created. 
 * It will be created further.
 * 
 * @author Patrick Tessier
 *
 */

public class Validation {

	/** Error message string */
	private static String VALIDATION_ERROR =
		"The model Papyrus tries to open is ill-formed.\n" +
			"You can try to fix the issues with Papyrus or with a text editor.\n";
	
	/**
	 * this method is used to validate the Eboject with the {@link AdapterFactory}
	 * @param eObject   the EObject to validate
	 * @param adapterFactory  the adapterfactory
	 * @return true is the EObject is well build by taking account the level of validation
	 */
	public static boolean validate(EObject eObject,AdapterFactory adapterFactory){
		// if it contains one element the factory is well built
		if(eObject.eContents().size()==0){return true;}
		
		//in the case where the file contains only a model or a profile or a diagram
		if (eObject.eContents().size()==1&& (eObject.eContents().get(0).eContents().size()==0)){return true;}
		IPreferenceStore store = PapyrusPlugin.getDefault().getPreferenceStore();	
		
		// look for validation preference
		String validationLevel = store.getString(ValidationPreferenceConstants.VALIDATION_LEVEL);

		// if the level is none, the validation is not useful
		if( ValidationPreferenceConstants.NO_VALIDATION.equals(validationLevel)){return true;}

		// creation of a diagnostic
		PapyrusDiagnostician diagnostician= new PapyrusDiagnostician(adapterFactory);
		Diagnostic diagnostic =diagnostician.validate(eObject);

		int severity = diagnostic.getSeverity();
		String title = null;
		String message = null;
		//prepartion of comment see ValidationAction of EMF
		if (severity == Diagnostic.ERROR || severity == Diagnostic.WARNING)
		{
			title = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_title");
			message = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationProblems_message");
		}
		else
		{
			title = EMFEditUIPlugin.INSTANCE.getString("_UI_ValidationResults_title");
			message = EMFEditUIPlugin.INSTANCE.getString(severity == Diagnostic.OK ? "_UI_ValidationOK_message" : "_UI_ValidationResults_message");
		}

		int result = 0;
		//no problem so we return
		if (diagnostic.getSeverity() == Diagnostic.OK){return true;}

		// we have warning and the level of validation is high so we display information
		else if(severity == Diagnostic.WARNING&&
				validationLevel.equals(ValidationPreferenceConstants.VALIDATION_HIGH_LEVEL)){
			message= VALIDATION_ERROR +	message;
			result = DiagnosticDialog.openProblem
			(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, message, diagnostic);

		}
		//we have errors and the level is low or high
		else if(severity==Diagnostic.ERROR&&
				(validationLevel.equals(ValidationPreferenceConstants.VALIDATION_HIGH_LEVEL)||
						(validationLevel.equals(ValidationPreferenceConstants.VALIDATION_LOW_LEVEL)))){
			message= VALIDATION_ERROR + message;
			result = DiagnosticDialog.openProblem
			(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), title, message, diagnostic);
		}
		if( severity==Diagnostic.ERROR){return false;}
		else{return true;}

	}



	public static boolean validate(EObject eObject, ModelManager modeManager){
		AdapterFactory adapterFactory= modeManager.getAdapterFactory();
		return validate(eObject, adapterFactory);
	}	
}
